package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月26日
*时间：上午10:19:58
*/
public class Q64 {
    public void mergeSortedArray(int[] A, int m, int[] B, int n) {
        if (B == null || B.length == 0) {
            return;
        }
        int pointA = 0;
        int pointB = 0;
        while (pointA < m && pointB < n) {
            if (A[pointA] > B[pointB]) {
                insert(A, pointA, B[pointB], m);
                m++;
                pointB++;
            } else {
                pointA++;
            }
        }
        while (pointB < n) {
            A[pointA++] = B[pointB++];
        }

    }

    private void insert(int[] a, int pointA, int value, int m) {
        for (int i = m; i > pointA; i--) {
            a[i] = a[i - 1];
        }
        a[pointA] = value;
    }

    @Test
    public void testA() {
        int a[] = { 1, 3, 4, 6, 0, 0 };
        int b[] = { 2, 5 };
        int m = 4;
        int n = 2;
        mergeSortedArray(a, m, b, n);

    }
}
